<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.getScript()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/ajax/">Ajax</a> &gt; <a href="/category/ajax/shorthand-methods/">快捷方法</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/jQuery.getScript/" target="_blank">jQuery.getScript()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="jQuery-getScript1"><h2 class="section-title">
<span class="name">jQuery.getScript( url [, success(script, textStatus, jqXHR) ] )</span><span class="returns">返回: <a href="/Types/#jqXHR">jqXHR</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>
			  使用一个HTTP GET请求从服务器加载并执行一个 JavaScript 文件</p>
<ul class="signatures"><li class="signature" id="jQuery-getScript-url-successscript--textStatus--jqXHR">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>jQuery.getScript( url [, success(script, textStatus, jqXHR) ] )</h4>
<ul>
<li>
<div><strong>url</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>一个包含发送请求的URL字符串。</div>
</li>
<li>
<div><strong>success(script, textStatus, jqXHR)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>当请求成功后执行的回调函数。</div>
</li>
</ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>这是一个Ajax函数的缩写，这相当于:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.ajax({</code></div></div><div class="container"><div class="line"><code>  url: url,</code></div></div><div class="container"><div class="line"><code>  dataType: <span class="string">"script"</span>,</code></div></div><div class="container"><div class="line"><code>  success: success</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>这个脚本在全局环境中执行，所以指向其他变量和运行jQuery函数。被加载的脚本同样作用于当前页面：</p>
<h4 id="success-callback">
			        Success Callback（成功回调）
			      </h4>
<p>一旦脚本已经被加载，将触发回调
			 但不一定执行。</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">".result"</span>).html(<span class="string">"&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;"</span>);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>通过引用这个文件名，脚本被包含进来并执行：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.getScript(<span class="string">"ajax/test.js"</span>, <span class="keyword">function</span>(data, textStatus, jqxhr) {</code></div></div><div class="container"><div class="line"><code>   console.log(data); <span class="comment">//data returned</span></code></div></div><div class="container"><div class="line"><code>   console.log(textStatus); <span class="comment">//success</span></code></div></div><div class="container"><div class="line"><code>   console.log(jqxhr.status); <span class="comment">//200</span></code></div></div><div class="container"><div class="line"><code>   console.log(<span class="string">'Load was performed.'</span>);</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4 id="handling-errors">Handling Errors（错误处理）</h4>
<p>
			从jQuery 1.5开始，你可以用<a href="/deferred.fail"><code>.fail()</code></a>来处理错误:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.getScript(<span class="string">"ajax/test.js"</span>)</code></div></div><div class="container"><div class="line"><code>.done(<span class="keyword">function</span>(script, textStatus) {</code></div></div><div class="container"><div class="line"><code>  console.log( textStatus );</code></div></div><div class="container"><div class="line"><code>})</code></div></div><div class="container"><div class="line"><code>.fail(<span class="keyword">function</span>(jqxhr, settings, exception) {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">"div.log"</span> ).text( <span class="string">"Triggered ajaxError handler."</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>
			      jQuery1.5之前，不得不使用全局的<code>.ajaxError()</code>回调事件来处理<code>$.getScript()</code>错误：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"div.log"</span> ).ajaxError(<span class="keyword">function</span>(e, jqxhr, settings, exception) {</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">if</span> (settings.dataType==<span class="string">'script'</span>) {</code></div></div><div class="container"><div class="line"><code>    $(<span class="keyword">this</span>).text( <span class="string">"Triggered ajaxError handler."</span> );</code></div></div><div class="container"><div class="line"><code>  }</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4 id="caching-requests">Caching Responses（缓存响应）</h4>
<p>
			
			      默认情况下，<code>$.getScript()</code> cache选项被设置为 <code>false</code>。这将在请求的URL的查询字符串中追加一个时间戳参数，以确保每次浏览器下载的脚本被重新请求。您可以全局的使用 <a href="/jquery.ajaxsetup"><code>$.ajaxSetup()</code></a>设置cache(缓存)属性覆盖该功能：
			
			    </p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.ajaxSetup({</code></div></div><div class="container"><div class="line"><code>  cache: <span class="literal">true</span></code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>另外，
			 你可以使用更灵活的 <code>$.ajax()</code> 方法定义一个新的方法
			 </p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4>Example: <span class="desc">定义了一个 $.cachedScript() 方法可以获取缓存的脚本：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>jQuery.cachedScript = <span class="keyword">function</span>(url, options) {</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  <span class="comment">// allow user to set any option except for dataType, cache, and url</span></code></div></div><div class="container"><div class="line"><code>  options = $.extend(options || {}, {</code></div></div><div class="container"><div class="line"><code>    dataType: <span class="string">"script"</span>,</code></div></div><div class="container"><div class="line"><code>    cache: <span class="literal">true</span>,</code></div></div><div class="container"><div class="line"><code>    url: url</code></div></div><div class="container"><div class="line"><code>  });</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  <span class="comment">// Use $.ajax() since it is more flexible than $.getScript</span></code></div></div><div class="container"><div class="line"><code>  <span class="comment">// Return the jqXHR object so we can chain callbacks</span></code></div></div><div class="container"><div class="line"><code>  <span class="keyword">return</span> jQuery.ajax(options);</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Usage</span></code></div></div><div class="container"><div class="line"><code>$.cachedScript(<span class="string">"ajax/test.js"</span>).done(<span class="keyword">function</span>(script, textStatus) {</code></div></div><div class="container"><div class="line"><code>  console.log( textStatus );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">
			我们动态加载新的<a href="http://github.com/jquery/jquery-color">官方jQuery 颜色动画插件</a>，一旦该插件加载完成就会触发一些颜色动画。</span>
</h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
<div class="line n36">36</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code><span class="class">.block</span> <span class="rules">{</span></code></div></div><div class="container"><div class="line"><code>   <span class="rule"><span class="attribute">background-color</span>:<span class="value"> blue;</span></span></code></div></div><div class="container"><div class="line"><code>   <span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="number">150</span>px;</span></span></code></div></div><div class="container"><div class="line"><code>   <span class="rule"><span class="attribute">height</span>:<span class="value"> <span class="number">70</span>px;</span></span></code></div></div><div class="container"><div class="line"><code>   <span class="rule"><span class="attribute">margin</span>:<span class="value"> <span class="number">10</span>px;</span></span></code></div></div><div class="container"><div class="line"><code><span class="rule">}</span><span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"go"</span>&gt;</span>&amp;raquo; Run<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"block"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">var</span> url = <span class="string">"https://raw.github.com/jquery/jquery-color/master/jquery.color.js"</span>;</code></div></div><div class="container"><div class="line"><code>  $.getScript(url, <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"#go"</span>).click(<span class="keyword">function</span>(){</code></div></div><div class="container"><div class="line"><code>      $(<span class="string">".block"</span>)</code></div></div><div class="container"><div class="line"><code>        .animate( { backgroundColor: <span class="string">"rgb(255, 180, 180)"</span> }, <span class="number">1000</span> )</code></div></div><div class="container"><div class="line"><code>        .delay(<span class="number">500</span>)</code></div></div><div class="container"><div class="line"><code>        .animate( { backgroundColor: <span class="string">"olive"</span> }, <span class="number">1000</span> )</code></div></div><div class="container"><div class="line"><code>        .delay(<span class="number">500</span>)</code></div></div><div class="container"><div class="line"><code>        .animate( { backgroundColor: <span class="string">"#00f"</span> }, <span class="number">1000</span> );</code></div></div><div class="container"><div class="line"><code>    });</code></div></div><div class="container"><div class="line"><code>  });</code></div></div><div class="container"><div class="line"><code>})();</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>